package com.freak.wanandroid.model.search.detail;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.freak.wanandroid.R;
import com.freak.wanandroid.base.activity.BaseLifecycleActivity;
import com.freak.wanandroid.bean.SearchDetailBean;
import com.freak.wanandroid.model.home.activity.ArticleDetailAct;
import com.freak.wanandroid.model.login.LoginActivity;
import com.freak.wanandroid.model.search.adapter.SearchDetailAdapter;
import com.freak.wanandroid.net.CommonLiveData;
import com.freak.wanandroid.util.ToastUtil;
import com.freak.wanandroid.util.ToolUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 74099 on 2018/9/6.
 */

public class SearchDetailActivity extends BaseLifecycleActivity<SearchDetailViewModel> {
    private String mSearchMsg;

    private LinearLayout mSearchDetailNoData;

    private XRecyclerView mSearchDetailRecycle;

    private int mPage = 0;

    private List<SearchDetailBean.Datas> mListBeans;

    private SearchDetailAdapter mSearchDetailAdapter;

    public static void startAction(Context context, String searchMsg) {
        Intent intent = new Intent(context, SearchDetailActivity.class);
        intent.putExtra("searchMsg", searchMsg);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_search_detail;
    }

    @Override
    protected void initEventAndData() {
        mSearchMsg = getIntent().getStringExtra("searchMsg");
        mListBeans = new ArrayList<>();
//        setBackPress();
//        setTitleTx(searchMsg);
        mSearchDetailNoData = findViewById(R.id.search_detail_no_data);
        mSearchDetailRecycle = findViewById(R.id.search_detail_recycle);
        mSearchDetailRecycle.setLayoutManager(new LinearLayoutManager(this));
        mSearchDetailRecycle.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPage = 0;
                mViewModel.getSearchDetail(mPage, mSearchMsg);
            }

            @Override
            public void onLoadMore() {
                mPage++;
                mViewModel.getSearchDetail(mPage, mSearchMsg);
            }
        });
        mPage = 0;
        mViewModel.getSearchDetail(mPage, mSearchMsg);
    }

    @Override
    protected void onDestroyRelease() {

    }

    @Override
    protected void initView() {

    }

    private void loading(List<SearchDetailBean.Datas> mList) {
        if (mList != null && mList.size() != 0) {
            mSearchDetailRecycle.loadMoreComplete();
            mListBeans.addAll(mList);
            mSearchDetailAdapter.setData(mListBeans);
            mSearchDetailAdapter.notifyDataSetChanged();

        } else {
            mSearchDetailRecycle.setNoMore(true);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSearchDetailRecycle.setNoMore(true);
                }
            }, 1000);
        }
    }

    private void refresh(List<SearchDetailBean.Datas> mList) {
        if (mList != null && mList.size() != 0) {
            mSearchDetailNoData.setVisibility(View.GONE);
            mSearchDetailRecycle.setVisibility(View.VISIBLE);
            mListBeans.clear();
            mListBeans.addAll(mList);
            if (mSearchDetailAdapter == null) {
                mSearchDetailAdapter = new SearchDetailAdapter(this, mListBeans);
                mSearchDetailRecycle.setAdapter(mSearchDetailAdapter);
                mSearchDetailAdapter.setOnItemClickListener(new SearchDetailAdapter.OnItemClickListener() {
                    @Override
                    public void doCollage(SearchDetailBean.Datas mData, ImageView imageView) {
                        if (mData.getCollect()) {
                            mViewModel.doCancelCollect(mData.getId());
                        } else {
                            mViewModel.doCollect(mData.getId());
                        }
                    }

                    @Override
                    public void doIntern(SearchDetailBean.Datas mData) {
                        ArticleDetailAct.startAction(SearchDetailActivity.this, mData.getTitle(), mData.getLink());
                    }
                });
            } else {
                mSearchDetailAdapter.setData(mListBeans);
                mSearchDetailAdapter.notifyDataSetChanged();
            }
            mSearchDetailRecycle.refreshComplete();
        } else {
            mSearchDetailNoData.setVisibility(View.VISIBLE);
            mSearchDetailRecycle.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onCreateLoadData() {

    }

    @Override
    protected void onResumeLoadData() {

    }

    @Override
    protected void dataObserver() {
        mViewModel.mDoCollect.observe(this, this::doCollectSuccessOrError);
        mViewModel.mDoCancelCollect.observe(this, this::doCancelCollectSuccessOrError);
        mViewModel.mGetSearchDetail.observe(this, this::getSearchDetailSuccess);
        mViewModel.mGetSearchDetailError.observe(this, this::getSearchDetailError);
    }

    /**
     * 收藏成功或失败处理
     *
     * @param commonLiveData CommonLiveData
     */
    public void doCollectSuccessOrError(CommonLiveData commonLiveData) {
        if (CommonLiveData.SUCCESS == commonLiveData.code) {
            mPage = 1;
            mViewModel.getSearchDetail(mPage, mSearchMsg);
            ToastUtil.showShort(this, "收藏成功");
            return;
        }
        ToastUtil.showShort(this, "登录过期，请重新登录");
        ToolUtils.logout(this);
        LoginActivity.startAction(this);
    }

    public void doCancelCollectSuccessOrError(CommonLiveData commonLiveData) {
        if (CommonLiveData.SUCCESS == commonLiveData.code) {
            mPage = 1;
            mViewModel.getSearchDetail(mPage, mSearchMsg);
            ToastUtil.showShort(this, "取消收藏成功");
            return;
        }
        ToastUtil.showShort(this, "登录过期，请重新登录");
        ToolUtils.logout(this);
        LoginActivity.startAction(this);
    }


    /***
     * 获取成功回调
     */
    public void getSearchDetailSuccess(SearchDetailBean searchDetailBeanList) {
        List<SearchDetailBean.Datas> datas = searchDetailBeanList.getDatas();
        if (mPage == 0) {
            refresh(datas);
        } else {
            loading(datas);
        }
    }

    public void getSearchDetailError(String msg) {
        ToastUtil.showShort(this, msg);
    }
}
